package com.renwei.yunlong.pager;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.MeUpcomingAllAdapter;
import com.renwei.yunlong.adapter.OwnerMeUpcomingAllAdapter;
import com.renwei.yunlong.base.BaseFragment;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.OwnerUpcomingAllBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.bean.UpcomingAllBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;

import java.util.List;


/**
 * 我的待办的全部--页面
 * <p>
 * Created by Administrator on 2017/9/6.
 */

public class MeUpcomingAllPager extends BaseFragment implements View.OnClickListener,HttpTaskListener{

    private RecyclerView recyclerView;

    private OwnerMeUpcomingAllAdapter ownerAdapter;
    private MeUpcomingAllAdapter adapter;
    private ServiceLoginBean serviceBean;
    private LoginBean loginBean;
    private ACache mCache;

    private String companyType;

    @Override
    public void onClick(View v) {

    }

    @Override
    public View initView() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_me_upcoming_all, null);

        mCache = ACache.get(getActivity());
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

        return view;
    }

    private void setDataList(List<UpcomingAllBean.Rows> mList){
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        adapter = new MeUpcomingAllAdapter(getActivity(),mList);

        recyclerView.setAdapter(adapter);
    }

    private void setOwnerDataList(List<OwnerUpcomingAllBean.Rows> mList){
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        ownerAdapter = new OwnerMeUpcomingAllAdapter(getActivity(),mList);

        recyclerView.setAdapter(ownerAdapter);
    }

    @Override
    public void initData() {
        companyType = mCache.getAsString("companyType");
        loginBean = (LoginBean) mCache.getAsObject("loginBean");
        serviceBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
        if ("2".equals(companyType)){
            //{"currentUserId":当前登录人员ID,"kind":查询种类（0：全部，1：工单，2：问题，3：投诉，4：备件）,startDate:筛选开始日期,endDate:筛选结束日期，ownerCode:筛选业主，contractId:筛选项目,"content":"查询条件",page:第几页,rows:每页显示条数}
          if (serviceBean != null){
                if (loginBean != null){
                    String jsonBean = "{currentUserId:\"" + serviceBean.getRows().getEmployeeId() + "\",kind:\"" + "0" + "\",startDate:\"" + "" + "\",endDate:\"" + "" + "\",ownerCode:\"" + "" + "\",contractId:\"" + "" + "\",content:\"" + "" + "\",page:\"" + "1" + "\",rows:\"" + "100000" + "\"}";
                    serviceUpcomingAllHttp(jsonBean);
                }
            }

        }else if ("1".equals(companyType)){

            String jsonBean = "{currentUserId:\"" + loginBean.getRows().getEmployeeId() + "\",kind:\"" +"0"+"\",startDate:\"" +""+"\",endDate:\"" +""+"\",serviceProviderCode:\"" +""+"\",content:\"" +""+"\",page:\"" +"1"+"\",rows:\"" +"100000"+"\"}";
            OwnerUpcomingAllHttp(jsonBean);
        }

    }

    //服务商
    private void serviceUpcomingAllHttp(String jsonBean){
        HttpPresenter hp = new HttpPresenter(getActivity());
        hp.setUrl(Api.SERVICE_UPCOMING_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean",jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //业主
    private void OwnerUpcomingAllHttp(String jsonBean){
        HttpPresenter hp = new HttpPresenter(getActivity());
        hp.setUrl(Api.OWNER_UPCOMING_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean",jsonBean);
        hp.setCallBack(this);
        hp.post();

    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId){
            case 1:
                UpcomingAllBean upBean = new Gson().fromJson(result,UpcomingAllBean.class);
                if (upBean.getMessage().getCode() == 200){
                    setDataList(upBean.getRows());
                }
                break;
            case 2:
                OwnerUpcomingAllBean ownerBean = new Gson().fromJson(result,OwnerUpcomingAllBean.class);
                if (ownerBean.getMessage().getCode() == 200){
                    setOwnerDataList(ownerBean.getRows());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
